package org.jeecg.modules.wlhy.setting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.modules.wlhy.setting.entity.HySetting;
import org.jeecg.modules.wlhy.setting.mapper.HySettingMapper;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: hy_setting
 * @Author: luruirui
 * @Date:   2021-04-23
 * @Version: V1.0
 */
@Service
@Slf4j
public class HySettingServiceImpl extends ServiceImpl<HySettingMapper, HySetting> implements IHySettingService {
    @Override
    public HySetting getByTag(String tag){
        QueryWrapper <HySetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag", tag);
        return this.getOne(queryWrapper);
    }

    @Override
    @CacheEvict(cacheNames=CacheConstant.CACHE_SETTING, key="#setting.tag")
    public boolean edit(HySetting setting) {
        return this.updateById(setting);
    }

    @Override
    @Cacheable(unless="#result == null", cacheNames= CacheConstant.CACHE_SETTING, key="#p0")
    public String getSettingValueByTag(String tag){
        HySetting setting = this.getByTag(tag);
        if (setting == null) {
//            return "未找到tag为" + tag + "的设置项！";
            return "";
        }
        //根据设置类型返回相应值
        //设置类型(1开关 2文件 3图片 4其它)
//        if (setting.getType() == 1){
//            return setting.getData().equals("1");//开关设置返回boolen值
//        }else{
        return setting.getData();
//        }
    }

    @Override
    @Cacheable(unless="#result == null", cacheNames=CacheConstant.CACHE_SETTING, key="#p0")
    public String getSettingValueByTag2(String tag) {
        HySetting setting = this.getByTag(tag);
        if (setting == null) {
            return "";
        }
        return setting.getData();
    }

    @Override
    @Cacheable(unless="#result == null", cacheNames=CacheConstant.CACHE_SETTING_LIST)
    public List<HySetting> getList() {
       return   this.list();
    }

    @Override
    @CacheEvict(cacheNames=CacheConstant.CACHE_SETTING_LIST)
    public void clearCache() {

    }


}
